package com.zw.yzk.kabu.view.activity.imagedetails;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.domain.interactor.DefaultOnNext;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.ExecutionException;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class OriginalImageDetailsActivity extends ImageDetailsActivity {


    public static void startOriginalDetailsActivity(Context from, String picUrl, Rect fromRect) {
        if (TextUtils.isEmpty(picUrl)) {
            return;
        }
        Intent intent = new Intent(from, OriginalImageDetailsActivity.class);
        intent.putExtra("url", picUrl);
        intent.putExtra("fromRect", fromRect);
        from.startActivity(intent);
        //禁用过渡动画
        if (from instanceof Activity) {
            ((Activity) from).overridePendingTransition(0, 0);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_original;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.view_original).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                downloadOriginalImage();
            }
        });
    }

    public void downloadOriginalImage() {

        Observable.create(new Observable.OnSubscribe<Bitmap>() {
            @Override
            public void call(Subscriber<? super Bitmap> subscriber) {
                try {
                    File file = Glide.with(OriginalImageDetailsActivity.this)
                            .load(getEnterImageUrl())
                            .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .get();
                    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
                    subscriber.onNext(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultOnNext<Bitmap>() {
                    @Override
                    public void call(Bitmap bitmap) {
                        image.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}

